const Koa = require('koa');
const path = require('path');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');
const config = require('./server-config');
const statics = require('koa-static');

const isDev = false;

const dbTools = require('./databaseTool');

const resObj = (data,code,msg)=>{
    return{
	code:code?code:200,
	data:data?data:"",
	msg:msg?msg:""
    }
};

let allowUpdate = true;
if(!isDev){
    setInterval(()=>{
	if(allowUpdate){
	    allowUpdate = false;
	    dbTools.updateBlock().then(()=>{
		allowUpdate = true;
	    },(err)=>{
		allowUpdate = true;
	    })
	}
    },config.autoUpdateTime);
}

const Koa_server = new Koa();
const Koa_router = new Router();

Koa_router.get('/explorer/getBlockHigh',async (ctx,next)=>{
    if(allowUpdate){
	dbTools.updateBlock().then(()=>{});
    }
    return dbTools.getBlockHigh().then((data)=>{
	ctx.body = resObj(data);
    });
});
Koa_router.get('/explorer/getBlockList',async (ctx,next)=>{
    return dbTools.getBlockByShowCount(6).then((data)=>{
	ctx.body = resObj(data);
    });
});
Koa_router.get('/explorer/getTxList',async (ctx,next)=>{
    // return dbTools.getTxList(6).then((data)=>{
    return dbTools.getOverviewTxList(6).then((data)=>{
	ctx.body = resObj(data);
    });
});
Koa_router.post('/explorer/getTxInfo',(ctx,next)=>{
    let body = ctx.request.body;
    return dbTools.getTxInfo(body.txHex.toString().trim()).then((data)=>{
	ctx.body = resObj(data);
    },(err)=>{
	ctx.body = resObj("",400,err);
    });
});
Koa_router.post('/explorer/getBlockInfoByHigh',(ctx,next)=>{
    let body = ctx.request.body;
    return dbTools.getBlockInfoByHigh(body.value).then((data)=>{
	ctx.body = resObj(data);
    });
});
Koa_router.post('/explorer/getBlockInfoByHex',(ctx,next)=>{
    let body = ctx.request.body;
    return dbTools.getBlockInfoByHex(body.value).then((data)=>{
	ctx.body = resObj(data);
    });
});
Koa_router.get('/explorer/getPeerList',(ctx,next)=>{
    return dbTools.getPeerList().then((data)=>{
	ctx.body = resObj(data);
    });
});
Koa_router.post('/explorer/getMemory',(ctx,next)=>{
    let body = ctx.request.body;
    return dbTools.getMemory(body.ip).then((data)=>{
	ctx.body = resObj(data);
    });
});
Koa_router.post('/explorer/getDisk',(ctx,next)=>{
    let body = ctx.request.body;
    return dbTools.getDisk(body.ip).then((data)=>{
	ctx.body = resObj(data);
    });
});
Koa_router.post('/explorer/getCPU',(ctx,next)=>{
    let body = ctx.request.body;
    return dbTools.getCpu(body.ip).then((data)=>{
	ctx.body = resObj(data);
    });
});
Koa_router.post('/explorer/getNetwork',(ctx,next)=>{
    let body = ctx.request.body;
    return dbTools.getNetwork(body.ip).then((data)=>{
	ctx.body = resObj(data);
    });
});


Koa_router.get('*', async ctx => {
    // ctx.respond = false
});
Koa_server.use(async (ctx, next) => {
    ctx.res.statusCode = 200;
    await next()
});
Koa_server.use(statics(path.join(__dirname, '../public')));

Koa_server.use(bodyParser());
Koa_server.use(Koa_router.routes());
Koa_server.listen(config.webPort, (err) => {
    if (err) throw err;
    console.log(`> Ready on http://localhost:${config.webPort}`)
})
